package com.cloudcreativity.blyg.utils;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.base.BaseBindingRecyclerViewAdapter;
import com.cloudcreativity.blyg.databinding.ItemLayoutDialogCategoryOneBinding;
import com.cloudcreativity.blyg.databinding.ItemLayoutDialogCategoryTwoBinding;
import com.cloudcreativity.blyg.databinding.LayoutDialogCategoryBinding;
import com.cloudcreativity.blyg.sqlite.CategoryOne;
import com.cloudcreativity.blyg.sqlite.CategoryTwo;
import com.cloudcreativity.blyg.sqlite.utils.CategoryDaoUtils;

import java.util.List;

public class CategoryDialog extends Dialog {

    public BaseBindingRecyclerViewAdapter<CategoryOne, ItemLayoutDialogCategoryOneBinding> oneAdapter;
    public BaseBindingRecyclerViewAdapter<CategoryTwo, ItemLayoutDialogCategoryTwoBinding> twoAdapter;

    private CategoryOne currentOne;
    private CategoryTwo currentTwo;

    public ObservableField<String> select = new ObservableField<>();

    private OnOkListener onOkListener;

    public void setOnOkListener(OnOkListener onOkListener) {
        this.onOkListener = onOkListener;
    }

    CategoryDialog(@NonNull Context context) {
        super(context, R.style.myProgressDialogStyle);
        setCanceledOnTouchOutside(false);
        setCancelable(true);
        LayoutDialogCategoryBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context),
                R.layout.layout_dialog_category,
                null,
                false);
        binding.setUtils(this);
        setContentView(binding.getRoot());
        binding.rcvOne.setLayoutManager(new LinearLayoutManager(context,
                LinearLayoutManager.VERTICAL,false));
        binding.rcvTwo.setLayoutManager(new LinearLayoutManager(context,
                LinearLayoutManager.VERTICAL,false));

        final List<CategoryOne> allCategories = new CategoryDaoUtils(context).getAllCategories();

        oneAdapter = new BaseBindingRecyclerViewAdapter<CategoryOne, ItemLayoutDialogCategoryOneBinding>(context) {
            @Override
            protected int getLayoutResId(int viewType) {
                return R.layout.item_layout_dialog_category_one;
            }

            @Override
            protected void onBindItem(ItemLayoutDialogCategoryOneBinding binding, final CategoryOne item, final int position) {
                binding.setItem(item);
                binding.getRoot().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        currentOne = item;
                        twoAdapter.getItems().clear();
                        twoAdapter.getItems().addAll(item.getSeconds());
                        select.set(item.getName());
                    }
                });
            }
        };
        twoAdapter = new BaseBindingRecyclerViewAdapter<CategoryTwo, ItemLayoutDialogCategoryTwoBinding>(context) {
            @Override
            protected int getLayoutResId(int viewType) {
                return R.layout.item_layout_dialog_category_two;
            }

            @Override
            protected void onBindItem(ItemLayoutDialogCategoryTwoBinding binding, final CategoryTwo item, int position) {
                binding.setItem(item);
                binding.getRoot().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        currentTwo = item;
                        select.set(currentOne.getName()+"/"+item.getName());
                    }
                });
            }
        };
        oneAdapter.getItems().addAll(allCategories);
        assert getWindow() != null;
        getWindow().getAttributes().width = context.getResources().getDisplayMetrics().widthPixels/2;
    }

    public void onOk(){
        if(null != currentOne && null != currentTwo){
            onOkListener.onOk(currentOne,currentTwo);
        }
        dismiss();
    }

    public void onClose(){
        dismiss();
    }

    public interface OnOkListener{
        void onOk(CategoryOne one,CategoryTwo two);
    }
}
